package com.br.command.impl;

import org.apache.log4j.Logger;
import org.json.JSONObject;

import com.br.command.BaseCommand;
import com.br.socket.ConnectionWS;
import com.br.socket.GameSocket;
import com.br.utils.BufferUtils;

/**
 * direction 1 = right
 * direction 2 = left
 */
public class MoveMe implements BaseCommand {
	
	private Logger logger = Logger.getLogger(this.getClass());

	public void process(ConnectionWS me, JSONObject json) {
		
		try{
			int direction = json.getInt("direction");
			
			int x = me.getPlayer().getTileX();
			int y = me.getPlayer().getTileY();
			
			int toX = me.getPlayer().getTileX();
			int toY = me.getPlayer().getTileY();
			
			if(direction == 1){
				toX++;
			}else if (direction == 2){
				toX--;
			}
			
			me.getPlayer().setTileX(toX);
			me.getPlayer().setTileY(toY);
			
			JSONObject msg = BufferUtils.montarMovimento(me.getPlayer().getUsername(), x, y, toX, toY, direction);
			
			GameSocket.broadcast(msg.toString(), me);
		
		}catch(Exception e){
			logger.error("Erro ao processar MoveMe",e);
		}
	}

}

